package qujian;

import java.util.ArrayList;
import java.util.List;

/**
 * 缺少的区间
 * 输入: nums = [0, 1, 3, 50, 75], lower = 0 和 upper = 99,
 * 输出: ["2", "4->49", "51->74", "76->99"]
 *
 * @author luolidong
 * @version 1.0
 * @create 2021-08-03 19:11
 */
public class L163 {
    public List<String> findMissingRanges(int[] nums, int lower, int upper) {
        List<String> list = new ArrayList<>();
        if (nums[0] != lower) {
            merge(list, lower, nums[0] - 1);
        }
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] - 1 != nums[i - 1]) {
                merge(list, nums[i - 1] + 1, nums[i] - 1);
            }
        }
        //添加最后的
        if (nums[nums.length - 1] != upper) {
            merge(list, nums[nums.length - 1] + 1, upper);
        }
        return list;
    }

    public void merge(List<String> list, int start, int end) {
        if (start == end) {
            list.add(String.valueOf(start));
        } else {
            list.add(start + "->" + end);
        }
    }

    public static void main(String[] args) {
        L163 l163 = new L163();
        List<String> missingRanges = l163.findMissingRanges(new int[]{0, 1, 3, 50, 75}, 0, 100);
        System.out.println(missingRanges);
    }
}
